﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SST_BL;
using System.Data;

namespace TCU
{
    public partial class Reportes : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) {
                SqlDataSource sqldtCircuito = new SqlDataSource();
                sqldtCircuito.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["SSTConnectionString"].ToString();
                sqldtCircuito.SelectCommand = "SELECT CircuitoID, NombreCircuito FROM Circuito WHERE FechaBorrado IS NULL";
                dropCircuito.DataSource = sqldtCircuito;
                dropCircuito.DataTextField = "NombreCircuito";
                dropCircuito.DataValueField = "CircuitoID";
                dropCircuito.DataBind();
                dropCircuito.Items.Insert(0, new ListItem("-Seleccionar-", "-1"));
                dropCircuito.SelectedIndex = 0;
            }
            CargarReporte();
        }

        private void CargarReporte() {
            SqlDataSource sqldt = new SqlDataSource();
            sqldt.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["SSTConnectionString"].ToString();
            int FiltroCircuito = 0;
            if(dropCircuito.Items.Count > 0)
                FiltroCircuito = int.Parse(dropCircuito.SelectedValue);
            int FiltroHabilidad = 0;
            if(dropHabilidad.Items.Count > 0)    
                FiltroHabilidad = int.Parse(dropHabilidad.SelectedValue);

            string selectCommand = "SELECT DISTINCT a.AsignacionID,a.Circuito, a.Habilidad, a.Informatico, a.RealizadaPor ,a.Fecha "
                                    +" FROM Asignacion as a ";
            if(FiltroCircuito > 0)
                selectCommand += " INNER JOIN InformaticoCircuito as ic ON a.Circuito=ic.CircuitoID "
                                +" INNER JOIN Circuito as c ON ic.CircuitoID=c.CircuitoID ";
            if (FiltroHabilidad > 0)
                selectCommand += " INNER JOIN InformaticoHabilidad as ih ON a.Informatico=ih.InformaticoID "
                            +" INNER JOIN Habilidad as h ON ih.HabilidadID=h.HabilidadID ";

            if(FiltroCircuito > 0 || FiltroHabilidad > 0) 
                selectCommand += " WHERE ";
            
            if(FiltroCircuito > 0)
                selectCommand += "c.CircuitoID = "+FiltroCircuito+" ";
            if (FiltroHabilidad > 0)
            {
                if (FiltroCircuito > 0)
                    selectCommand += " AND ";
                selectCommand += "h.HabilidadID = " + FiltroHabilidad+" ";
            }

            //selectCommand += " a.FechaBorrado IS NULL";
            sqldt.SelectCommand = selectCommand;
            gvReporte.DataSource = sqldt;
            //DataTable dt = gvReporte.DataSource as DataTable;
            gvReporte.DataBind();
        }

        protected void dropCircuito_SelectedIndexChanged(object sender, EventArgs e)
        {
            SqlDataSource sqldtHabilidad = new SqlDataSource();
            sqldtHabilidad.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["SSTConnectionString"].ToString();
            sqldtHabilidad.SelectCommand = "SELECT HabilidadID, NombreHabilidad "
                                            + " FROM Habilidad as h "
                                            + " WHERE FechaBorrado IS NULL AND Circuito = " + int.Parse(dropCircuito.SelectedValue);
            dropHabilidad.DataSource = sqldtHabilidad;
            dropHabilidad.DataTextField = "NombreHabilidad";
            dropHabilidad.DataValueField = "HabilidadID";
            dropHabilidad.DataBind();
            dropHabilidad.Items.Insert(0, new ListItem("-Seleccionar-", "-1"));
            dropHabilidad.SelectedIndex = 0;
            CargarReporte();
        }

        protected void dropHabilidad_SelectedIndexChanged(object sender, EventArgs e)
        {
            CargarReporte();
        }

        protected void dropInformatico_SelectedIndexChanged(object sender, EventArgs e)
        {

        }


        
    }
}